<?php


namespace app\app\controller;


class Feedback extends Base
{
    public function __construct()
    {
        parent::__construct();
    }

    public function feedback()
    {
        $input = input();
        if (empty($input['text']) && empty($input['fujianurl'])) {
            $this->error('请填写反馈内容或者上传反馈截屏');
        }
        if (empty($input['type'])) {
//            $this->error('请选择反馈类型');
            $input['type'] = '未填写';
        }
        $device_id = $input['device_id'];
        if (empty($device_id)) {
            $this->error('未能获取道您的设备信息，提交失败');
        }
        $dataText='';
        if(empty($input['text'])){
            switch ($input['fujiantype']){
                case 'pic':
                    $dataText = '图片附件'.$input['fujianurl'];
                    break;
                case 'vod':
                    $dataText = '视频附件'.$input['fujianurl'];
                    break;
            }
        }else{
            $dataText=$input['text'];
        }
//        if (empty($device_id)) {
//            dingding_curl(
//                [
//                    "msgtype" => "text",
//                    "text" => [
//                        "content" => '[影视意见反馈 设备信息出错]' . json_encode($input, JSON_UNESCAPED_UNICODE),
//                    ],
//                    "at" => [
//                        "isAtAll" => true
//                    ]
//                ]
//            );
//            $this->error('设备信息有误');
//        } else {
//            dingding_curl(
//                [
//                    "msgtype" => "text",
//                    "text" => [
//                        "content" => '[影视意见反馈]' . json_encode($input, JSON_UNESCAPED_UNICODE),
//                    ],
//                    "at" => [
//                        "isAtAll" => true
//                    ]
//                ]
//            );
//        }
//            {"type":"播放问题",
//                "text":"WIFI播放也卡",
//                "contact":"1343145749",
//                "_ajax":1,
//                "jiami":-1,
//                "language":"zh-CN",
//                "app_version":"0.0.45",
//                "sys_version":"10",
//                "version":"1.3.63",
//                "system_type":"android",
//                "device_id":"51547ff9f8efe2ae",
//                "device_model":"V1824A",
//                "device_name":"vivo",
//                "ui_mode":"phone",
//                "operator":"中国联通",
//                "connection_type":"wifi",
//                "is_proxy":0
//            }
        $currentTime = time();
        //检查是否存在该设备反馈信息
        $isExistData = \db('feedback')->where('device_id', $input['device_id'])->find();
        if (!$isExistData) {
            $insertArray = [
                'app_id' => $input['app_id'],
                'app_name' => $input['app_name'],
                'device_name' => $input['device_name'],
                'app_version' => $input['app_version'],
                'contact' => $input['contact'],
                'time_added' => $currentTime,
                'time_updated' => $currentTime,
                'content' => $dataText,
                'type' => $input['type'],
                'device_id' => $input['device_id'],
            ];
            //保存数据库
            $feedBackId = \db('feedback')->insertGetId($insertArray);
        } else {
            $feedBackId = $isExistData['id'];
            //更新管理员未读状态
            \db('feedback')->where('id', $feedBackId)->update([
                'is_admin_read' => 0,
                'app_id' => $input['app_id'],
                'app_name' => $input['app_name'],
                'device_name' => $input['device_name'],
                'time_updated' => $currentTime,
                'content' => $dataText,
                'app_version' => $input['app_version']
            ]);
        }
        //写入log表
        $logArray = [
            'app_id' => $input['app_id'],
            'app_name' => $input['app_name'],
            'device_id' => $input['device_id'],
            'device_name' => $input['device_name'],
            'content' => $input['text'],
            'type' => $input['type'],
            'app_version' => $input['app_version'],
            'contact' => $input['contact'],
            'time_added' => $currentTime,
            'fujiantype' => $input['fujiantype'],
            'fujianurl' => $input['fujianurl'],
        ];

        db('feedback_log')->insertGetId($logArray);


        if ($feedBackId > 0)
            $this->success('反馈成功！');
        else
            $this->error('数据出错！');
    }

    /**
     * 根据device_id获取列表数据
     */
    public function index()
    {
        $device_id = input('device_id');
        $app_id = input('app_id');
        if (empty($device_id)) {
            $this->error('数据有误！');
        }
        $where = [];
        $lastId = input('lastId', 0, 'intval');
        if (!empty($lastId) && $lastId > 0) {
            $where['id'] = ['<', $lastId];
        }
        $list = db('feedback_log')->where($where)->where('device_id', $device_id)->where('app_id', $app_id)->order('time_added', 'desc')->paginate(1000);
        $list->each(function ($item) {
            $item['time_added_text'] = date('m-d H:i:s', $item['time_added']);
            return $item;
        });
        $currentPage = $list->currentPage();
        $lastPage = $list->lastPage();
        if ($currentPage === $lastPage) {
            $hasMore = false;
        } else {
            $hasMore = true;
        }

        $listArray = $list->items();
        $this->success('获取成功！', [
            'list' => $listArray,
            'lastId' => $listArray[count($listArray) - 1]['id'],
            'hasMore' => $hasMore
        ]);


    }

    public function getLastIndex()
    {
        $device_id = input('device_id');
        if (empty($device_id)) {
            $this->error('数据有误！');
        }
        $firstId = input('firstId');
        if (empty($firstId)) {
            $this->error('数据有误！');
        }

        $list = db('feedback_log')->where('device_id', $device_id)->where('id', '>', $firstId)->order('id', 'asc')->select();

        $this->success('success', ['list' => $list]);

    }

    public function setRead()
    {
        $device_id = input('device_id');
        if (empty($device_id)) {
            $this->error('数据有误！');
        }
        db('feedback')->where('device_id', $device_id)->update(['is_user_read' => 1]);

        db('feedback_log')->where('device_id', $device_id)->where('is_read', 0)->where('is_reply', 1)->update(['is_read' => 1]);

        $this->success('success');
    }

    public function getUnreadNum()
    {
        $device_id = input('device_id');
        if (empty($device_id)) {
            $this->error('数据有误！');
        }
        $nums = db('feedback_log')->where('is_reply', 1)->where('device_id', $device_id)->where('is_read', 0)->count();
        if ($nums < 1) {
            $nums = 0;
        }
        $this->success('success', ['num' => $nums]);
    }


//    public function fixedDeviceInfo(){
//        $list = db('feedback')->column('device_id');
//        foreach ($list as $device_id){
//            $deviceInfo = db('device')->where('device_id',$device_id)->find();
//            dump(json_encode($deviceInfo,JSON_UNESCAPED_UNICODE));
//            if(!empty($deviceInfo)) {
//                db('feedback')->where('device_id', $device_id)->update([
//                    'app_id' => $deviceInfo['app_id'],
//                    'app_name' => $deviceInfo['app_name'],
//                    'device_name' => $deviceInfo['device_name'],
//                ]);
//                db('feedback_log')->where('device_id', $device_id)->update([
//                    'app_id' => $deviceInfo['app_id'],
//                    'app_name' => $deviceInfo['app_name'],
//                    'device_name' => $deviceInfo['device_name'],
//                ]);
//            }
//        }
//    }
}

